package com.learn.project2.service.admin.keyword;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.project2.bean.MarketKeyword;
import com.learn.project2.bean.MarketKeywordExample;
import com.learn.project2.mapper.MarketKeywordMapper;
import com.learn.project2.model.bo.admin.keyword.KeywordCreateBO;
import com.learn.project2.model.bo.admin.keyword.KeywordDeleteBO;
import com.learn.project2.model.bo.admin.keyword.KeywordUpdateBO;
import com.learn.project2.model.vo.admin.keyword.KeywordCreateVO;
import com.learn.project2.model.vo.admin.keyword.KeywordListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 曹源
 * @version 1.0
 * @description: 商场管理/关键词 Admin/keyword的接口方法实现
 * @date 2022/1/7 9:12
 */
@Service
//@CacheConfig(cacheNames ={"marketKeyword"})
public class KeywordServiceImpl implements KeywordService {
    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    /**
     * @description: 商场管理/关键词/查询 Admin/keyword/list的Service的方法实现
     * @param:
     * @return:
     * @author 曹源
     * @date: 2022-01-07 10:50
     */
    @Override
//    @Cacheable
    public KeywordListVO selectList(Integer page, Integer limit, String keyword, String url, String sort, String order) {
        KeywordListVO keywordListVO = new KeywordListVO();
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        //根据keyword和url做一个模糊查询
        MarketKeywordExample example = new MarketKeywordExample();
        // 提供了一个条件List → Criterion
        //criteria中包含List<Criterion> 使用Criteria提供的方法就是向List塞入条件
        //提供的方法是和数据库表的列名相关的
        example.setOrderByClause(sort + "\n" + order);
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        //and 列名 条件
        if (keyword != null) criteria.andKeywordLike("%" + keyword + "%");
        if (url != null) criteria.andUrlLike("%" + url + "%");
        criteria.andDeletedEqualTo(false);
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(example);
        PageInfo<MarketKeyword> pageInfo = new PageInfo<>(marketKeywords);
        //映射赋值VO
        keywordListVO.setTotal((int) pageInfo.getTotal());
        keywordListVO.setPages(pageInfo.getPages());
        keywordListVO.setPage(page);
        keywordListVO.setLimit(limit);
        keywordListVO.setList(marketKeywords);
        return keywordListVO;
    }

    /**
     * @description: 商场管理/关键词/添加 Admin/keyword/create的Service的方法实现
     * @param:
     * @return:
     * @author 曹源
     * @date: 2022-01-07 17:07
     */
    @Override
    public KeywordCreateVO insert(KeywordCreateBO keywordCreateBO) {
        KeywordCreateVO keywordCreateVO = new KeywordCreateVO();
        //建立pojo的添加语句
        MarketKeyword marketKeyword = new MarketKeyword();
        marketKeyword.setKeyword(keywordCreateBO.getKeyword());
        marketKeyword.setUrl(keywordCreateBO.getUrl());
        marketKeyword.setIsHot(keywordCreateBO.getIsHot());
        marketKeyword.setIsDefault(keywordCreateBO.getIsDefault());
        marketKeyword.setAddTime(new Date());
        marketKeyword.setUpdateTime(new Date());
        marketKeywordMapper.insertSelective(marketKeyword);
        // 建立查询的返回语句
        MarketKeyword marketKeyword1 = marketKeywordMapper.selectByPrimaryKey(marketKeyword.getId());
        return keywordCreateVO.add(marketKeyword1);
    }

    /**
     * @description: 商场管理/关键词/编辑 Admin/keyword/update的Service的方法实现
     * @param:
     * @return:
     * @author 曹源
     * @date: 2022-01-07 17:07
     */
    @Override
    public MarketKeyword update(KeywordUpdateBO keywordUpdateBO) {
        //建立输入映射
        MarketKeyword marketKeyword = new MarketKeyword();
        marketKeyword.setKeyword(keywordUpdateBO.getKeyword());
        marketKeyword.setUrl(keywordUpdateBO.getUrl());
        marketKeyword.setIsHot(keywordUpdateBO.getIsHot());
        marketKeyword.setIsDefault(keywordUpdateBO.getIsDefault());
        marketKeyword.setId(keywordUpdateBO.getId());
        marketKeyword.setUpdateTime(new Date());
        marketKeywordMapper.updateByPrimaryKeySelective(marketKeyword);

        MarketKeyword marketKeyword1 = marketKeywordMapper.selectByPrimaryKey(marketKeyword.getId());
        return marketKeyword1;
    }

    /**
     * @description: 商场管理/关键词/删除 Admin/keyword/delete的Service的方法实现
     * @param:
     * @return:
     * @author 曹源
     * @date: 2022-01-07 22:23
     */
    @Override
    public void delete(KeywordDeleteBO keywordDeleteBO) {
        MarketKeyword marketKeyword = new MarketKeyword();
        marketKeyword.setId(keywordDeleteBO.getId());
        marketKeyword.setDeleted(true);
        marketKeywordMapper.updateByPrimaryKeySelective(marketKeyword);
    }
}
